package com.jrx.anytxn.param.service.limit.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.dto.limit.CreditLimitTypeReq;
import com.jrx.anytxn.param.dto.limit.CreditLimitTypeRes;
import com.jrx.anytxn.param.entity.PrCreditLimitType;
import com.jrx.anytxn.param.mapper.ext.ExtPrCreditLimitTypeMapper;
import com.jrx.anytxn.param.service.limit.ICreditLimitTypeService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author by yf
 * @Classname CreditLimitTypeServiceImpl
 * @Description 额度节点实现类
 * @Date 2019/6/12 0012 下午 21:05
 */
@Service
public class CreditLimitTypeServiceImpl implements ICreditLimitTypeService {
    private static final Logger logger = LoggerFactory.getLogger(CreditLimitTypeServiceImpl.class);


    @Resource
    private ExtPrCreditLimitTypeMapper extPrCreditLimitTypeMapper;

    @Autowired
    private ISegmentService segmentService;

    @Override
    public TxnPage<CreditLimitTypeRes> findAllPageByTenId(Integer pageNum, Integer pageSize, String tenantId) throws TxnException {
        Page<PrCreditLimitType> page = PageHelper.startPage(pageNum, pageSize,Constant.ORDER_BY_SORT);
        List<PrCreditLimitType> prCreditLimitTypeList = extPrCreditLimitTypeMapper.selectByTenId(tenantId);
        if (prCreditLimitTypeList.isEmpty()) {
            logger.error("查询的额度节点信息列表为可空");
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }
        List<CreditLimitTypeRes> res = BeanMapping.copyList(prCreditLimitTypeList, CreditLimitTypeRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }

    @Override
    public CreditLimitTypeRes findLimitTypeByTenIdAndLimitId(String tenantId, String limitId) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(limitId)) {
            logger.error("查询额度节点时额度节点主键ID或租户ID为空");
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PrCreditLimitType prCreditLimitType = extPrCreditLimitTypeMapper.selectByTenIdAndLimitId(tenantId, limitId);
        return BeanMapping.copy(prCreditLimitType, CreditLimitTypeRes.class);
    }

    @CacheEvict(value = "CREDIT_LIMIT", key = "'creditLimit_'+#tenantId+'_'+#limitId")
    @Override
    public Boolean removeTenIdAndLimitId(String tenantId, String limitId) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(limitId)) {
            logger.error("删除额度节点时主键ID或租户ID为空");
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        int a = extPrCreditLimitTypeMapper.deleteByTenIdAndLimitId(tenantId, limitId);
        return a > 0;
    }

    @Override
    public CreditLimitTypeRes addCustomerLimitInfoTable(CreditLimitTypeReq creditLimitTypeReq) throws TxnException {
        PrCreditLimitType prCreditLimitType = BeanMapping.copy(creditLimitTypeReq, PrCreditLimitType.class);
        /*根据类型ID和组织ID判断在数据库中是否已存在*/
        PrCreditLimitType type = extPrCreditLimitTypeMapper.selectByTenIdAndLimitIdAndOrgId(prCreditLimitType.getTenantId(), prCreditLimitType.getLimitId(), prCreditLimitType.getOrganizationId());
        if (type != null) {
            logger.error("增加的额度节点信息已存在");
            throw new TxnException(TxnRespCode.DATA_EXISTS, TxnRespCode.DATA_EXISTS.getMsg());
        }
        try {
            prCreditLimitType.setId(segmentService.getId(Constant.PR_CREDIT_LIMIT_TYPE));
        } catch (SegmentException e) {
            logger.error("增加额度节点时主键生成错误",e);
            throw new TxnException(ErrorMsgConstant.ERROR_ID);
        }
        prCreditLimitType.setRecordVersionNumber(Constant.VERSION_NUMBER);
        extPrCreditLimitTypeMapper.insertSelective(prCreditLimitType);
        return BeanMapping.copy(prCreditLimitType, CreditLimitTypeRes.class);
    }

    @CacheEvict(value = "CREDIT_LIMIT",key = "'creditLimit_'+#creditLimitTypeReq.tenantId+'_'+#creditLimitTypeReq.limitId")
    @Override
    public CreditLimitTypeRes modifyCustomerLimitInfoTable(CreditLimitTypeReq creditLimitTypeReq) throws TxnException {
        PrCreditLimitType prCreditLimitType = BeanMapping.copy(creditLimitTypeReq, PrCreditLimitType.class);
        /*根据租户ID和节点ID判断此信息是否存在 ，不存在则抛出异常，否则修改不为空的字段信息*/
        PrCreditLimitType creditLimitType = extPrCreditLimitTypeMapper.selectByTenIdAndLimitId(prCreditLimitType.getTenantId(), prCreditLimitType.getLimitId());
        if (creditLimitType == null) {
            logger.error("修改时此额度节点信息不存在");
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }
        extPrCreditLimitTypeMapper.updateByTenIdAndLimitId(prCreditLimitType);
        return BeanMapping.copy(prCreditLimitType, CreditLimitTypeRes.class);
    }

    @Override
    public List<CreditLimitTypeRes> findAllLimitTypeByTenId(String tenantId) {
        List<PrCreditLimitType> prCreditLimitTypeList = extPrCreditLimitTypeMapper.selectByTenId(tenantId);
        return BeanMapping.copyList(prCreditLimitTypeList, CreditLimitTypeRes.class);
    }

    @Override
    public List<CreditLimitTypeRes> findAllLimitTypeByOrgIdAndTenId(String orgId, String tenantId) throws TxnException {
        if (StringUtils.isBlank(orgId) || StringUtils.isBlank(tenantId)) {
            logger.error("额度节点机构号或租户ID为空");
            throw new TxnException(TxnRespCode.NOT_EMPTY);
        }
        List<PrCreditLimitType> prCreditLimitTypeList = extPrCreditLimitTypeMapper.selectByOrgIdAndTenId(orgId, tenantId);
        return BeanMapping.copyList(prCreditLimitTypeList, CreditLimitTypeRes.class);
    }

    @Override
    public CreditLimitTypeRes findByLimitIdAndOrgIdAndTenId(String limitId, String orgId, String tenantId) throws TxnException {
        if (StringUtils.isBlank(limitId) || StringUtils.isBlank(orgId)||StringUtils.isBlank(tenantId)) {
            logger.error("额度节点ID或机构号或租户ID为空");
            throw new TxnException(TxnRespCode.NOT_EMPTY);
        }
        PrCreditLimitType prCreditLimitType = extPrCreditLimitTypeMapper.selectByTenIdAndLimitIdAndOrgId(tenantId, limitId, orgId);
        return BeanMapping.copy(prCreditLimitType, CreditLimitTypeRes.class);
    }

    @Cacheable(value = "CREDIT_LIMIT", key = "'creditLimit_'+#tenantId+'_'+#limitId")
    @Override
    public String getUserModal(String limitId, String organizationId, String tenantId) {
        return extPrCreditLimitTypeMapper.selectUserModal(limitId, organizationId, tenantId);
    }

}
